<!--盘库页面-->
<template>
  <div class="app-container">
    <el-form ref="form" inline :model="form" class="form-options" label-width="100px">
      <el-form-item label="任务号" prop="checkNo">
        <el-input v-model="form.checkNo" placeholder="请输入任务号"  clearable/>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入任务名称"  clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态"  filterable clearable>
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发起时间" class="long-content" prop="createTime">
        <el-date-picker
          v-model="form.createTime"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          align="right"
        />
      </el-form-item>

      <el-form-item label="盘点时间" class="long-content" prop="checkTime">
        <el-date-picker
          v-model="form.checkTime"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          align="right"
        />
      </el-form-item>

      <div class="btns">
        <el-button type="primary" @click="onQuery">查询</el-button>
        <el-button type="success" @click="handleAdd">新增</el-button>
        <el-button @click="resetFormSearch('form')">重置</el-button>
      </div>
    </el-form>
    <!--列表-->
    <el-table
      v-loading="loading"
      :data="tableData"
      header-cell-class-name="table-header"
      style="width: 100%;margin-top: 20px"
      show-overflow-tooltip
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        prop="checkNo"
        label="任务号"
      />
      <el-table-column
        prop="name"
        label="名称"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        prop="statusName"
        label="状态"
      />
      <el-table-column
        prop="lowNum"
        label="缺少数量"
      />
      <el-table-column
        prop="highNum"
        label="多出数量"
      />
      <el-table-column
        prop="createName"
        label="创建人"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
      <el-table-column
        prop="checkTime"
        label="盘点完成时间"
      />
    </el-table>

    <!--分页-->
    <div style="text-align: right;margin-top: 15px">
      <el-pagination
        background
        :current-page="currentPage"
        :page-sizes="[10, 15, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 弹窗 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="300px"
    >
      <el-form ref="diaForm" inline :model="diaForm" :rules="rules" class="form-options">
        <el-form-item label="名称" prop="name">
          <el-input v-model="diaForm.name" placeholder="请输入任务名称" clearable maxlength="30"/>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="onSubmit()">保存</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCheckQuery,
  getCheckAdd
} from '@/api/inventoryMake';

export default {
  name: 'InventoryMake',
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      statusOptions: [{
        value: 0,
        label: '未完成'
      }, {
        value: 1,
        label: '完成'
      }],
      form: {
        name: '',
        status: '', // 状态
        checkNo: '',
        createTime: null,
        checkTime: null
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      loading: false,
      tableData: [],
      dialogVisible: false,
      diaForm: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.onQuery();
  },
  methods: {
    // 分页
    handleSizeChange: function(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getTableData();
    },
    handleCurrentChange: function(val) {
      this.currentPage = val;
      this.getTableData();
    },
    // 查询
    onQuery() {
      console.log('查询', this.form)
      this.currentPage = 1;
      this.getTableData();
    },
    // 查询盘库列表数据
    getTableData() {
      this.loading = true;
      const { createTime, checkTime }  = this.form;
      if(createTime && createTime.length> 0){
        this.$set(this.form, 'createTimeStart', createTime[0]);
        this.$set(this.form, 'createTimeEnd', createTime[1]);
      } else {
        this.$set(this.form, 'createTimeStart', '');
        this.$set(this.form, 'createTimeEnd', '');
      }
      if(checkTime && checkTime.length> 0){
        this.$set(this.form, 'checkTimeStart', checkTime[0]);
        this.$set(this.form, 'checkTimeEnd', checkTime[1]);
      } else {
        this.$set(this.form, 'checkTimeStart', '');
        this.$set(this.form, 'checkTimeEnd', '');
      }
      const params = {
        page: this.currentPage,
        rows: this.pageSize,
        ...this.form
      };
      getCheckQuery(params).then(res => {
        this.tableData = res.resultData;
        this.total = res.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },
    // 新增
    handleAdd() {
      this.dialogVisible = true;
      this.resetForm("diaForm");
    },
    // 新增提交
    onSubmit() {
      this.$refs["diaForm"].validate((valid) => {
        if (valid) {
          const { name } = this.diaForm;
          getCheckAdd({name}).then(res => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.onQuery();
            this.dialogVisible = false;
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 搜索重置
    resetFormSearch(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
        this.currentPage = 1;
        this.onQuery();
      })
    },
    // 重置
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields();
        this.$refs[formName].clearValidate();
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}
.grid-content {
  margin-top: 20px;
  border-radius: 4px;
  text-align: center;
  min-height: 36px;
  line-height: 36px;
}
.bg-purple {
  background: #e5e9f2;
}
</style>

